/**
 *
 * Copyright by Samuel Castro & Joaquim Fonseca, 2011
 * All Rights Reserved
 *
 * NOTES:
 * Please do not redistribute this plugin without my permission
 * Plugin made for private use only
 *
 */
package com.SySammy.SdBountyHunter.Handlers;

import com.SySammy.SdBountyHunter.PersistenceDataBase;
import com.SySammy.SdBountyHunter.sdLaw;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import org.bukkit.entity.Player;

public class BountyHandler {

    sdLaw plugin;

    public BountyHandler(sdLaw plugin) {
        this.plugin = plugin;
    }

    private PersistenceDataBase OpenDataBase(String ply) {
        PersistenceDataBase persistenceClass = plugin.getDatabase().find(PersistenceDataBase.class).where().ieq("playerName", ply).findUnique();
        if (persistenceClass == null) {
            persistenceClass = new PersistenceDataBase();
        }
        return persistenceClass;
    }

    public boolean HasBounty(String ply) {
        PersistenceDataBase persistenceClass = plugin.getDatabase().find(PersistenceDataBase.class).where().ieq("playerName", ply).findUnique();
        if (persistenceClass == null) {
            persistenceClass = new PersistenceDataBase();
            return false;
        }
        return true;
    }

    public void MakeBounty(String badguy, String goodguy, int bounty, String reason) {
        PersistenceDataBase plyReg = OpenDataBase(badguy);
        plyReg.setBountymaker(goodguy);
        plyReg.setBounty(bounty);
        plyReg.setCaught(false);
        plyReg.setPlayerName(badguy);
        plyReg.setReason(reason);
        plugin.getDatabase().save(plyReg);
    }

    public int GetBounty(String ply) {
        PersistenceDataBase plyReg = OpenDataBase(ply);
        return plyReg.getBounty();
    }

    public String GetReason(String ply) {
        PersistenceDataBase plyReg = OpenDataBase(ply);
        return plyReg.getReason();
    }

    public ArrayList<ArrayList<Object>> GetBounties() {
        List<PersistenceDataBase> persistenceClass = plugin.getDatabase().find(PersistenceDataBase.class).where().findList();
        ArrayList<ArrayList<Object>> Plybounties = new ArrayList<ArrayList<Object>>();
        Iterator<PersistenceDataBase> iterator = persistenceClass.iterator();
        while (iterator.hasNext()) {
            PersistenceDataBase next = iterator.next();
            ArrayList<Object> bounties = new ArrayList<Object>();
            bounties.add(next.getPlayerName());
            bounties.add(next.getBounty());
            bounties.add(next.getReason());
            bounties.add(next.isCaught());
            Plybounties.add(bounties);
        }
        return Plybounties;
    }

    public void SetCaught(Player p) {
        PersistenceDataBase plyReg = OpenDataBase(p.getName());
        plyReg.setCaught(true);
        plugin.getDatabase().save(plyReg);
    }

    public boolean GetCaught(String ply) {
        PersistenceDataBase plyReg = OpenDataBase(ply);
        return plyReg.isCaught();
    }

    public String GetGoodGuy(String ply) {
        PersistenceDataBase plyReg = OpenDataBase(ply);
        return plyReg.getBountymaker();
    }

    public void RemoveBounty(String ply) {
        PersistenceDataBase plyReg = OpenDataBase(ply);
        plugin.getDatabase().delete(PersistenceDataBase.class, plyReg.getPlayerId());
    }

    public void RemoveaAllCaughts() {
        List<PersistenceDataBase> persistenceClass = plugin.getDatabase().find(PersistenceDataBase.class).findList();
        ListIterator<PersistenceDataBase> listIterator = persistenceClass.listIterator();
        while (listIterator.hasNext()) {
            PersistenceDataBase next = listIterator.next();
            next.setCaught(false);
            plugin.getDatabase().save(next);
        }
    }
}
